/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.user;


import com.nc.kyiv.yellow.sociallibrary.dao.UserDAO;
import com.nc.kyiv.yellow.sociallibrary.POJO.LibUser;
import com.nc.kyiv.yellow.sociallibrary.helper.MapBuilder;
import com.nc.kyiv.yellow.sociallibrary.similarUse.CommonClassesInterface;
import java.util.Map;

/**
 *
 * @author Evgeniy Grigorevskiy
 */
public class CheckLoginAndPass implements  CommonClassesInterface{
    private String [] mapKeys = {"login","pass"};
    private MapBuilder helper = new MapBuilder();
    private String result;
    
    public CheckLoginAndPass(){
        
    }
    
    public String resultCode(LibUser user){        
        StringBuilder coded = new StringBuilder();
        coded.append(user.getId());
        coded.append(helper.getResponceSplitter());
        coded.append(user.getLogin());
        coded.append(helper.getResponceSplitter());
        coded.append(user.getPass());
        coded.append(helper.getResponceSplitter());
        coded.append(user.getFirstName());
        coded.append(helper.getResponceSplitter());
        coded.append(user.getRole());
        
        return coded.toString();
    }
    @Override
    public String execute(String parameters) throws Exception{
        
        Map postParameters= helper.stringToMap(parameters, mapKeys, helper.getRequestSplitter());
        
        if (postParameters!=null){
            //instance DAO and get LibUser

            UserDAO dao = new UserDAO();
            LibUser result = dao.getUserByLoginAndPass(postParameters);
            if (result!=null){
                return resultCode(result);
            } else {
                return "failed";
            }  
        } else {
           return "failed"; 
        }
        
    }
}
